%% Produce Figure 4

addpath('functions');
addpath('data')
load('output\results_Figure4')
load('recession.mat');
load('spf.mat');

v2struct(settings);
v2struct(results);

%% Colour Scheme

% Coloured Figures

red = [1 0 0];
blue = [0 0 1];

% Black and White Figures

blue = [0.2 0.2 0.2];
red = [0 0 0];

%% Figure 4a - Posterior Estimate of Decomposition
annualise = 1;
        trends = nanmean(squeeze(Draws_Factors(:,1:trend,:)),3);

            hourstrend = means(3)+3*trends(:,2);
            productivity_trend = means(1)-means(3)+3*trends(:,1);


trend_percentiles = means(1) + (1+annualise*2)*(1+standardise*(stdevs(1)-1))*prctile(squeeze(sum(Draws_Factors(:,1:trend,:),2)),[5,16,50,84,95],2);

f = figure(1);
    
bar(dates(p+1:end),4.98.*recession(p+1:end),'FaceColor',[0.9 0.9 0.9],'EdgeColor',[0.9 0.9 0.9],'LineWidth',0.1)
hold on
plot(dates(p+1:end),trend_percentiles(:,3),'color',red,'LineWidth',2.5)
plot(dates(p+1:end),trend_percentiles(:,[2,4]),'LineStyle',':','color',blue,'LineWidth',1.5)
plot(dates(p+1:end),trend_percentiles(:,[1,5]),'LineStyle','--','color',blue,'LineWidth',1.5)

if max(red) == 0
    
plot(dates(p+1:end),productivity_trend,'color',[0.5 0.5 0.5],'LineWidth',2)
plot(dates(p+1:24:end),productivity_trend(1:24:end),'x','color',[0.5 0.5 0.5],'LineWidth',2)

plot(dates(p+1:end),hourstrend,'-.','color',[0.5 0.5 0.5],'LineWidth',2)
plot(dates(p+1:24:end),hourstrend(1:24:end),'o','color',[0.5 0.5 0.5],'LineWidth',2)
    
text(datenum(1963,1,1),2.5,{'Labor','Productivity'},'HorizontalAlignment','left','FontSize',14)
text(datenum(1975,11,1),1,{'Labor','Input'},'HorizontalAlignment','left','FontSize',14)
text(datenum(2005,11,1),3.5,{'Long-run','GDP'},'HorizontalAlignment','left','FontSize',14)

else

plot(dates(p+1:end),productivity_trend,'color',[0.5 0.8 0.4],'LineWidth',2.5)
plot(dates(p+1:end),hourstrend,'--','color',[1 0.7 0.0],'LineWidth',2.5)

end

hold off

datesToPlot = datenum(1950:10:2020,1,1);
YAxisTicks = [0:1:5];
set(gca,'XTick',datesToPlot,'GridLineStyle',':','YTick',YAxisTicks)
datetick('x','yyyy','keepticks')
xmin=datenum(1947,1,1);
xmax=datenum(2015,6,1);
xlim([xmin,xmax])
ymin = 0;
ymax = 5;
ylim([ymin,ymax])
grid on

figSize = [10 7];
set(f, 'PaperUnits', 'inches');
set(f, 'Units','inches');
set(f, 'PaperSize', figSize);
set(f, 'PaperPositionMode', 'auto');
set(f, 'Position', [0 0 figSize(1) figSize(2)])
set(gca, 'FontName', 'Times-Roman');
set(gca, 'FontSize', 20);
set(gca,'Layer','Top')
% set(gca,'OuterPosition',[-0.12 -.06 1.2 1.1]);

ti = get(gca,'TightInset');
set(gca,'Position',[1.1*ti(1) 1.1*ti(2) 0.99*(1-ti(3)-ti(1)) 0.99*(1-ti(4)-ti(2))]);
pos = get(gca,'Position');
ti = get(gca,'TightInset');

fname = strcat('output\Figure4a.pdf');
print('-dpdf', f, fname);

%% Figure 4b - Filtered vs. SPF

annualise = 1;
        
            trends = xtt(:,1:trend);
         
            hourstrend = means(3)+3*trends(:,2);
            productivity_trend = means(1)-means(3)+3*trends(:,1);


trend_percentiles = means(1) + (1+annualise*2)*(1+standardise*(stdevs(1)-1))*prctile(squeeze(sum(Draws_Factors(:,1:trend,:),2)),[5,16,50,84,95],2);

f = figure(2);
    
bar(dates(p+1:end),2.99.*recession(p+1:end),'FaceColor',[0.9 0.9 0.9],'EdgeColor',[0.9 0.9 0.9],'LineWidth',0.1)
hold on
% plot(dates(p+1:end),trend_percentiles(:,3),'r','LineWidth',2.5)
% plot(dates(p+1:end),trend_percentiles(:,[2,4]),'b','LineWidth',1.5)
% plot(dates(p+1:end),trend_percentiles(:,[1,5]),'--b','LineWidth',1.5)

if max(red) == 0
    
plot(dates(p+1:end),productivity_trend,'color','k','LineWidth',2)

plot(dates(p+1:end),hourstrend,'-.','color',[0.5 0.5 0.5],'LineWidth',2)

plot(dates(p+1:end),spf(p+1:end-6,4),'d','color','k','LineWidth',2.5,'MarkerSize',10)
plot(dates(p+1:end),spf(p+1:end-6,3)-spf(p+1:end-6,4),'s','color',[0.5 0.5 0.5],'LineWidth',2.5,'MarkerSize',10)
    
text(datenum(2002,1,1),2.7,{'Labor Productivity'},'HorizontalAlignment','left','FontSize',14)
text(datenum(2005,1,1),0.6,{'Labor Input'},'HorizontalAlignment','left','FontSize',14)

else


plot(dates(p+1:end),productivity_trend,'color',[0.5 0.8 0.4],'LineWidth',2.5)
plot(dates(p+1:end),hourstrend,'--','color',[1 0.7 0.0],'LineWidth',2.5)
plot(dates(p+1:end),spf(p+1:end-6,4),'d','color',[0.5 0.8 0.4],'LineWidth',2.5,'MarkerSize',10)
plot(dates(p+1:end),spf(p+1:end-6,3)-spf(p+1:end-6,4),'d','color',[1 0.7 0.0],'LineWidth',2.5,'MarkerSize',10)

end

hold off

datesToPlot = datenum(1950:10:2020,1,1);
YAxisTicks = [0:1:3];
set(gca,'XTick',datesToPlot,'GridLineStyle',':','YTick',YAxisTicks)
datetick('x','yyyy','keepticks')
xmin=datenum(1990,1,1);
xmax=datenum(2015,6,1);
xlim([xmin,xmax])
ymin = 0;
ymax = 3;
ylim([ymin,ymax])
grid on

figSize = [10 7];
set(f, 'PaperUnits', 'inches');
set(f, 'Units','inches');
set(f, 'PaperSize', figSize);
set(f, 'PaperPositionMode', 'auto');
set(f, 'Position', [0 0 figSize(1) figSize(2)])
set(gca, 'FontName', 'Times-Roman');
set(gca, 'FontSize', 20);
set(gca,'Layer','Top')

% set(gca,'OuterPosition',[-0.12 -.06 1.2 1.1]);

ti = get(gca,'TightInset');
set(gca,'Position',[1.1*ti(1) 1.1*ti(2) 0.99*(1-ti(3)-ti(1)) 0.99*(1-ti(4)-ti(2))]);
pos = get(gca,'Position');
ti = get(gca,'TightInset');

fname = strcat('output\Figure4b.pdf');
print('-dpdf', f, fname);



